import html2canvas from 'html2canvas'
import { createNamespacedHelpers } from 'vuex'
const { mapState, mapMutations } = createNamespacedHelpers('editor')
export default {
  data () {
    return {}
  },
  computed: {
    ...mapState(['scale', 'canvasImg'])
  },
  methods: {
    ...mapMutations(['handleSelectCanvas', 'handleCanvasImg']),
    handleCanvas () {
      this.handleSelectCanvas(false)
      setTimeout(() => {
        const element = document.querySelector('.go-chart-edit-content')
        const { width, height } = element.getBoundingClientRect()
        html2canvas(element, {
          width,
          height,
          allowTaint: false,
          backgroundColor: null,
          useCORS: true,
          scale: (1 / this.scale),
          ignoreElements: e => {
            if (element.contains(e)) {
              return false
            }
          },
        }).then(canvas => {
          const url = canvas.toDataURL('image/png')
          // this.src = url
          this['handleCanvasImg'](url)
        })
      }, 500)
    }
  }
}